package net.sansi.v3correctserver.util;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import net.sansi.v3correctserver.entity.image.RecCmmImage;
import net.sansi.v3correctserver.entity.image.RecImage;
import net.sansi.v3correctserver.exception.ServiceException;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

/**
 * Created by Fang
 * create time : 2024/11/1
 * IDE : IntelliJ IDEA
 * <p>
 * 打屏服务实现
 */

@Slf4j
public class ShowUtil {

    static {
        System.setProperty("java.awt.headless", "false");
    }

    static JFrame frame = null;

    /**
     * 将图片显示在屏幕上
     *
     * @param recImage 图片
     */
    public static void show(RecImage recImage) {
        if (recImage instanceof RecCmmImage) {
            show(((RecCmmImage) recImage).getImage());
        } else {
            int w = recImage.getWidth(), h = recImage.getHeight();
            RecCmmImage image = new RecCmmImage(w, h);
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    image.setColor(x, y, recImage.getPhotoColor(x, y));
                }
            }
            show(image);
        }
    }

    /**
     * 将图片显示在屏幕上
     *
     * @param imagePath 图片路径
     * @throws IOException
     */
    public static void show(String imagePath) throws IOException {
        show(new File(imagePath));
    }

    /**
     * 将图片显示在屏幕上
     *
     * @param file 图片文件
     * @throws IOException
     */
    public static void show(File file) throws IOException {
        show(ImageIO.read(file));
    }


    /**
     * 将图片显示在屏幕上
     *
     * @param image 图片对象
     */
    public static void show(Image image) {
        //System.setProperty("java.awt.headless", "false");
        close();
        screenListener();
        // 获取图形环境
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] screens = ge.getScreenDevices();
        // 检查是否有多个显示器
        if (screens.length < 2) {
            throw new ServiceException("未识别到扩展屏幕");
        }
        // 选择第二个显示器
        GraphicsDevice secondScreen = screens[1];
        // 创建一个全屏窗口
        frame = new JFrame(IdUtil.fastSimpleUUID());
        frame.setUndecorated(true); // 去掉边框
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 创建一个标签来显示图片
        JLabel label = new JLabel(new ImageIcon(image));
        frame.getContentPane().add(label, BorderLayout.CENTER);
        // 设置窗口在第二个显示器上全屏显示
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setResizable(false);
        frame.setLocationByPlatform(true); // 让窗口出现在正确的显示器上
        // 设置窗口不可聚焦
        frame.setFocusableWindowState(false);
        secondScreen.setFullScreenWindow(frame);
        frame.setVisible(true);
        ThreadUtil.sleep(1000);
    }

    /**
     * 关闭窗口
     */
    synchronized public static void close() {
        if (frame == null) return;
        frame.dispose();
        frame = null;
    }

    public static volatile boolean isListening = false;

    public static void screenListener() {
        if (isListening) return;
        isListening = true;
        new Thread(() -> {
            while (true) {
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                GraphicsDevice[] screens = ge.getScreenDevices();
                if (screens.length < 2) {
                    close();
                    isListening = false;
                    break;
                }
                ThreadUtil.sleep(5000);
            }
        }).start();
    }
}
